#!/usr/bin/python3
# -*- coding:utf-8 -*-

# ~Authors: chenwenke
from django.shortcuts import HttpResponse
from rest_framework.response import Response
from rest_framework.views import APIView
from api import models
from api.utils.response import BaseResponse
from api import serializer
from rest_framework.viewsets import ViewSet
from rest_framework.generics import GenericAPIView
from api.utils.components import Authentication
from api.utils.components import MyThrottle
from rest_framework.renderers import JSONRenderer


class CoursesClassifyViews(APIView):

    def get(self,request,*args,**kwargs):

        ret = BaseResponse()
        obj = models.CourseClassify.objects.all()
        classify = serializer.CoursesClassifySerializer(obj,many=True)
        ret.data = classify.data

        return Response(ret.dict)

class CoursesViews(APIView):

    ret = BaseResponse()

    def get(self,request,*args,**kwargs):

        sub_category = request.query_params.get("sub_category",'0')
        ordering = request.query_params.get("ordering",'0')

        course_obj = models.Course.objects.all()

        sub_category_list,ordering_list = [],['-popularity','price','-price']

        for item in course_obj:
            sub_category_list.append(str(item.id))
        # print(sub_category_list)
        if sub_category not in sub_category_list:

            if ordering not in ordering_list:
                bs = serializer.CourseSerializer(course_obj, many=True)
                self.ret.data = bs.data
                return Response(self.ret.dict)
            else:
                if ordering == "price":
                    course_obj = course_obj.order_by('price__now_price')
                    bs = serializer.CourseSerializer(course_obj, many=True)
                    self.ret.data = bs.data
                    return Response(self.ret.dict)
                elif ordering == "-price":
                    course_obj = course_obj.order_by('-price__now_price')
                    bs = serializer.CourseSerializer(course_obj, many=True)
                    self.ret.data = bs.data
                    return Response(self.ret.dict)
                else:
                    course_obj = course_obj.order_by(ordering)
                    bs = serializer.CourseSerializer(course_obj, many=True)
                    self.ret.data = bs.data
                    return Response(self.ret.dict)
        else:
            if ordering in ordering_list:
                if ordering == "-popularity":
                    course_obj = models.Course.objects.filter(course_classify_id=int(sub_category)).order_by(ordering)
                    bs = serializer.CourseSerializer(course_obj, many=True)
                    self.ret.data = bs.data
                    return Response(self.ret.dict)
                else:
                    if ordering == "price":
                        course_obj = models.Course.objects.filter(course_classify_id=int(sub_category)).all().order_by("price__now_price")
                        bs = serializer.CourseSerializer(course_obj, many=True)
                        self.ret.data = bs.data
                        return Response(self.ret.dict)
                    elif ordering == "-price":
                        course_obj = models.Course.objects.filter(course_classify_id=int(sub_category)).all().order_by("-price__now_price")
                        bs = serializer.CourseSerializer(course_obj, many=True)
                        self.ret.data = bs.data
                        return Response(self.ret.dict)
            else:
                course_obj = models.Course.objects.filter(course_classify_id=int(sub_category))
                bs = serializer.CourseSerializer(course_obj, many=True)
                self.ret.data = bs.data
                return Response(self.ret.dict)

class CourseDetailView(APIView):

    def get(self,request,*args,**kwargs):
        pk = kwargs.pop("pk")

        ret = BaseResponse()
        if isinstance(pk,str):
            pk = int(pk)

        obj = models.Course.objects.filter(id=pk).first()

        bs = serializer.CourseDetailSerializer(obj,many=False)

        ret.data = bs.data
        return Response(ret.dict)

